package com.slt.developkitweb.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 工具类，用于读取和处理多个类文件。
 */
public class ClassFileProcessor {

    /**
     * 处理多个类文件，并生成新的文件格式。
     *
     * @param originalFilePaths 原始文件路径列表
     * @param newFilePaths      新文件路径列表
     */
    public static void processFiles(Map<String, String> originalFilePaths, Map<String, String> newFilePaths) {
        for (Map.Entry<String, String> entry : originalFilePaths.entrySet()) {
            String originalFilePath = entry.getKey();
            String newFilePath = newFilePaths.get(entry.getKey());

            try (BufferedReader reader = new BufferedReader(new FileReader(originalFilePath));
                 FileWriter writer = new FileWriter(newFilePath)) {
                String line;
                StringBuilder contentBuilder = new StringBuilder();

                while ((line = reader.readLine()) != null) {
                    contentBuilder.append(line).append("\n");
                }

                String originalContent = contentBuilder.toString();
                String newContent = transformContent(originalContent);

                writer.write(newContent);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 转换文件内容。
     *
     * @param originalContent 原始文件内容
     * @return 转换后的新文件内容
     */
    private static String transformContent(String originalContent) {
        // 替换注释和添加 Lombok 注解
        Pattern pattern = Pattern.compile("(\\s*//\\s*(.*?))\n(\\s*private\\s+[^;]*;)");
        Matcher matcher = pattern.matcher(originalContent);

        StringBuffer transformedContent = new StringBuffer();
        while (matcher.find()) {
            String comment = matcher.group(1);
            String property = matcher.group(3);
            String annotation = "@ApiModelProperty(\"" + comment.substring(2).trim() + "\")\n" + property;
            matcher.appendReplacement(transformedContent, annotation);
        }
        matcher.appendTail(transformedContent);

        // 添加 @Data 注解
        String newContent = transformedContent.toString().replace("public class", "@Data\npublic class");

        return newContent;
    }

    public static void main(String[] args) {
        Map<String, String> originalFilePaths = new HashMap<>();
        Map<String, String> newFilePaths = new HashMap<>();

        originalFilePaths.put("/Users/shalongteng/giteeProjects/develop-kit/develop-kit-web/src/main/java/com/slt/developkitweb/util/dto/User.java", "/Users/shalongteng/giteeProjects/develop-kit/develop-kit-web/src/main/java/com/slt/developkitweb/util/cto/User.java");
        // 可以添加更多文件

        newFilePaths.putAll(originalFilePaths); // 相同的键值对

        processFiles(originalFilePaths, newFilePaths);
    }
}
